package main

import (
	"context"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/rest"
	"github.com/zeromicro/go-zero/rest/httpx"
	"github.com/zeromicro/go-zero/zrpc"
	"google.golang.org/grpc"
	"log"
	"net/http"
	"study-go-zero/greet"
)

func main() {
	start2()
}

func start2() {
	var serverConf zrpc.RpcServerConf
	conf.MustLoad("etc/greet-server.yaml", &serverConf)
	server := zrpc.MustNewServer(serverConf, func(server *grpc.Server) {
		greet.RegisterGreetServer(server, &exampleServer{})
	})
	defer server.Stop()
	server.Start()
}

type exampleServer struct {
	greet.UnimplementedGreetServer
}

func (e *exampleServer) Ping(ctx context.Context, req *greet.Request) (*greet.Response, error) {
	return &greet.Response{Pong: "yzp"}, nil
}

func start1() {
	var restConf rest.RestConf
	conf.MustLoad("etc/helloworld.yaml", &restConf)
	server, err := rest.NewServer(restConf)
	if err != nil {
		log.Fatalln(err)
		return
	}
	server.AddRoute(rest.Route{
		Method: http.MethodGet,
		Path:   "/hello/world",
		Handler: func(w http.ResponseWriter, r *http.Request) {
			logc.Info(context.Background(), "访问 url => /hello/world")
			httpx.OkJson(w, "hello world")
		},
	})
	defer server.Stop()
	server.Start()
}
